﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Data;
using PDAMain.Entity;

namespace PDAMain.DAL.Dao
{
    class SynchronizationDataDao : BaseDao
    {
        /// <summary>
        /// 数据同步(上传未同步成功的数据)
        /// </summary>
        public void UploadData()
        {
            try
            {
                string CustomerSql = "select CWOrder_No,status,updatedt,Comment from CustomerWork where status!='1'";
                //string CustomerSql = "select * from CustomerWork where status !='1'";
                string InspectionSql = "select ASSIGNMENTID,status,STATUSDATE,remark from InspectionWork where status='1'";
                string AmmeterSql = "select MPRId,EM_METER_ID,READDEGREE,READINGTIME from ELECTRICITYMETER where READDEGREE>0";
                string WaterSql = "select MPRId,WM_METER_ID,READDEGREE,READINGTIME from WATERMETER where READDEGREE>0";
                string RepairSql = "select WONUM,status,remark,STATUSDATE from repairWorkList where status!='1'";
                try
                {
                    DataTable Customer = base.executeDataSet(CustomerSql).Tables[0];
                    Execute_Customer(Customer);
                }
                catch { }
                try
                {
                    DataTable Inspection = base.executeDataSet(InspectionSql).Tables[0];
                    Execute_Inspection(Inspection);
                }
                catch { }
                try
                {
                    DataTable Ammeter = base.executeDataSet(AmmeterSql).Tables[0];
                    Execute_Ammeter(Ammeter);
                }
                catch { }
                try
                {
                    DataTable Water = base.executeDataSet(WaterSql).Tables[0];
                    Execute_Water(Water);
                }
                catch { }
                try
                {
                    DataTable Repair = base.executeDataSet(RepairSql).Tables[0];
                    Execute_Repair(Repair);
                }
                catch { }
            }
            catch (Exception ex)
            {

            }
        }

        //同步客服工单数据
        public void Execute_Customer(DataTable dt)
        {
            pdaWebReference.PdaService pda = base.getPdaWebService();
            for (int index = 0; index < dt.Rows.Count; index++)
            {
                string customerID = dt.Rows[index]["CWOrder_No"].ToString().Trim();
                string status = dt.Rows[index]["status"].ToString().Trim();
                string time = dt.Rows[index]["updatedt"].ToString().Trim();
                string remark = dt.Rows[index]["Comment"].ToString().Trim();
                try
                {
                    bool Flag = pda.updateWorkOrder(customerID, status, time, remark, Identity.dutyPerson);
                    if (Flag)
                    {
                        string str_Sql = "delete from CustomerWork where CWOrder_No='" + customerID + "'";
                        Flag = base.executeNonQuery(str_Sql);
                    }
                }
                catch (Exception ex) { }
            }
        }

        //同步巡检工单数据
        public void Execute_Inspection(DataTable dt)
        {
            pdaWebReference.PdaService pda = base.getPdaWebService();
            for (int index = 0; index < dt.Rows.Count; index++)
            {
                string inspectionID = dt.Rows[index]["ASSIGNMENTID"].ToString().Trim();
                string status = dt.Rows[index]["status"].ToString().Trim();
                string time = dt.Rows[index]["STATUSDATE"].ToString().Trim();
                string remark = dt.Rows[index]["remark"].ToString().Trim();
                try
                {
                    bool Flag = pda.updateCheckWorkOrder(inspectionID, status, remark, time, Identity.dutyPerson);
                    if (Flag)
                    {
                        string str_Sql = "delete from InspectionWork where ASSIGNMENTID='" + inspectionID + "'";
                        Flag = base.executeNonQuery(str_Sql);
                        if (Flag)
                        {
                            str_Sql = "delete from inspectionWorkDetail where ASSIGNMENTID='" + inspectionID + "'";
                            base.executeNonQuery(str_Sql);
                        }
                    }
                }
                catch (Exception ex) { }
            }
        }

        //同步电表数据
        public void Execute_Ammeter(DataTable dt)
        {
            pdaWebReference.PdaService pda = base.getPdaWebService();
            for (int index = 0; index < dt.Rows.Count; index++)
            {
                string MPRId = dt.Rows[index]["MPRId"].ToString().Trim();
                string EM_METER_ID = dt.Rows[index]["EM_METER_ID"].ToString().Trim();
                string degree = dt.Rows[index]["READDEGREE"].ToString().Trim();
                string completedt = dt.Rows[index]["READINGTIME"].ToString().Trim();
                try
                {
                    bool blFlag = pda.updateAmmeterReadingWorkOrder(MPRId, degree, completedt, Identity.dutyPerson);
                    if (blFlag)
                    {
                        string delsql = " DELETE FROM ELECTRICITYMETER WHERE EM_METER_ID='" + EM_METER_ID + "'";
                        bool delFlag = base.executeNonQuery(delsql);
                    }
                }
                catch (Exception ex) { }
            }
        }

        //同步水表数据
        public void Execute_Water(DataTable dt)
        {
            pdaWebReference.PdaService pda = base.getPdaWebService();
            for (int index = 0; index < dt.Rows.Count; index++)
            {
                string MPRId = dt.Rows[index]["MPRId"].ToString().Trim();
                string WM_METER_ID = dt.Rows[index]["WM_METER_ID"].ToString().Trim();
                string degree = dt.Rows[index]["READDEGREE"].ToString().Trim();
                string completedt = dt.Rows[index]["READINGTIME"].ToString().Trim();
                try
                {
                    bool blFlag = pda.updateWatermeterReadingWorkOrder(MPRId, degree, completedt, Identity.dutyPerson);
                    if (blFlag)
                    {
                        string delsql = " DELETE FROM WATERMETER WHERE WM_METER_ID='" + WM_METER_ID + "'";
                        //更新成功刪除記錄
                        bool delFlag = base.executeNonQuery(delsql);
                    }
                }
                catch (Exception ex) { }
            }
        }

        //同步维修工单数据
        public void Execute_Repair(DataTable dt)
        {
            pdaWebReference.PdaService pda = base.getPdaWebService();
            for (int index = 0; index < dt.Rows.Count; index++)
            {
                string repairID = dt.Rows[index]["WONUM"].ToString().Trim();
                string status = dt.Rows[index]["status"].ToString().Trim();
                string remark = dt.Rows[index]["remark"].ToString().Trim();
                try
                {
                    bool Flag = pda.updateRepairWorkOrder(repairID, status, remark, Identity.dutyPerson);
                    if (Flag)
                    {
                        string str_Sql = "delete from repairWorkList where WONUM='" + repairID + "'";
                        //更新成功刪除記錄
                        bool delFlag = base.executeNonQuery(str_Sql);
                    }
                }
                catch (Exception ex) { }
            }
        }
    }
}
